package com.simon.system.controller;

import java.util.List;

import com.simon.exception.OperationFailedException;
import com.simon.http.model.DoResponse;
import com.simon.http.model.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.simon.http.model.PResponse;
import com.simon.system.model.Dict;
import com.simon.system.service.DictService;
import com.simon.token.TokenValidate;
import com.simon.utils.SystemCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName DictController
 * @Description 字典控制层
 * @Author Jiajie.Mao
 * @Date 2018/6/5 9:38
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/dict")
@Api(value = "dict", tags = {"字典接口"})
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "添加字典", notes = "根据Dict对象创建字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "dict", value = "字典对象", required = true, dataType = "Dict")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @TokenValidate
    public DoResponse<?> addDict(@RequestBody Dict dict) {
        Dict dDict = dictService.findByCode(dict.getCode(), dict.getPid());
        if (dDict != null) {
            throw new OperationFailedException("该字典项已存在");
        } else {
            //插入数据库
            int res = dictService.addDict(dict);
            if (res > 0) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException();
            }
        }
    }

    @ApiOperation(value = "查询字典", notes = "根据id获取字典信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "字典id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getDictById(@RequestParam int id) {
        Dict dict = dictService.getDictById(id);
        if (dict != null) {
            return new DoResponse<>(dict);
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "查询字典列表", notes = "根据自定义条件分页查询字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "pageNum", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int")
    })
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @TokenValidate
    public PageResponse<?> queryDicts(@RequestParam String code, @RequestParam String name, @RequestParam int pageNum, @RequestParam int pageSize) {
        List<Dict> dicts = dictService.queryDicts(code, name, pageNum, pageSize);
        PResponse bPage = dictService.calPage(code, name, pageSize);
        if (dicts != null) {
            return new PageResponse<>(dicts,dicts.size(),bPage.getTotalElements(),bPage.getTotalPage());
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "删除字典", notes = "根据用户id删除字典信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "字典id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @TokenValidate
    public DoResponse<?> deleteDict(@RequestParam int id) {
        int res = dictService.deleteDict(id);
        if (res > 0) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "更新字典", notes = "更新字典信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "字典id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "dict", value = "字典对象", required = true, dataType = "Dict")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> updateDict(@RequestParam int id, @RequestBody Dict dict) {
        Dict dDict = dictService.getDictById(id);
        if (dDict != null) {
            int res = dictService.updateDict(dDict, dict);
            if (res > 0) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException();
            }

        } else {
            throw new OperationFailedException("不存在此数据");
        }
    }

    @ApiOperation(value = "查询子字典值", notes = "根据code获取子字典信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌",  paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "上级字典code", required = true, dataType = "String")
    })
    @RequestMapping(value = "/sublist", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> subDictlist(@RequestParam String code) {
        List<Dict> dicts = dictService.subDictList(code);
        if (dicts != null) {
            return new DoResponse<>(dicts);
        } else {
            throw new OperationFailedException();
        }
    }
}
